package com.breaktian.hybrid.webview;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.breaktian.hybrid.R;

import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaInterfaceImpl;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaWebViewImpl;
import org.apache.cordova.LOG;
import org.apache.cordova.engine.SystemWebChromeClient;
import org.apache.cordova.engine.SystemWebView;
import org.apache.cordova.engine.SystemWebViewClient;
import org.apache.cordova.engine.SystemWebViewEngine;

public class H5WeviewActivity extends AppCompatActivity {

    public static String EXTRA_WEB_URL = "web_url";


    private Intent mIntent;
    private String mWebUrl;
    private CordovaWebViewImpl mCordovaWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5_weview);
        mIntent = getIntent();
        init();
        loadUrl();
    }


    private void init() {
        SystemWebView systemWebview = (SystemWebView) findViewById(R.id.webview);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);

        LOG.setLogLevel(LOG.DEBUG);//设置日志级别
        ConfigXmlParser parser = new ConfigXmlParser();
        parser.parse(this);//这里会解析res/xml/config.xml配置文件
        mCordovaWebView = new CordovaWebViewImpl(new SystemWebViewEngine(systemWebview));//创建一个cordovawebview
        mCordovaWebView.init(new CordovaInterfaceImpl(this), parser.getPluginEntries(), parser.getPreferences());//初始化
        SystemWebViewEngine systemWebViewEngine = (SystemWebViewEngine) mCordovaWebView.getEngine();

        systemWebview.setWebViewClient(new SystemWebViewClient(systemWebViewEngine){

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                webView.loadUrl(url);   //在2.3上面不加这句话，可以加载出页面，在4.0上面必须要加入，不然出现白屏
                return true;
            }
        });

        systemWebview.setWebChromeClient(new SystemWebChromeClient(systemWebViewEngine){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
                if(newProgress>99){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                        }
                    },1000);
                }
                else{
                    progressBar.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    /**
     * 加载url
     * */
    private void loadUrl() {
        mWebUrl = mIntent.getStringExtra(EXTRA_WEB_URL);
//        mWebUrl = "file:///android_asset/www/index.html";//file:///android_asset/sample.html
        if(!TextUtils.isEmpty(mWebUrl)){
            mCordovaWebView.loadUrl(mWebUrl);
        }
    }


}
